import { defineStore } from 'pinia'

//请求接口
import {
	getUserInfo,postLogisticAlive
} from "@/service/index.js"

//跳转判断
import {outerHref} from '@/composables/outerHref.js'

//猎鹰
	// #ifdef APP-PLUS
import {aMapTrack} from '@/composables/aMapTrack.js'
	//#endif

//创建用户小仓库
const useUserStore = defineStore('User',{
  state:()=>{
    return {
			userInfo:null,
			photoData:{
				userId:null,
				imges:null
			},
			limits:{
				positioning:false,//定位权限
			},
			startTrackTrue:true,//权限打开然后打开猎鹰定位
			listData:[],//列表数据
    }
  },
	actions:{
		//token登录
		loginUser(res,type=true){
			uni.setStorageSync('token',res);
			if(type){
				// this.getUserInfoFun();
				// this.appStart();
				//#ifdef APP-PLUS || H5
				return uni.showToast({
					title: '登录成功',
					icon: 'none',
					complete: () => {
						outerHref('/pages/index/index',2)
					}
				});
				//#endif
				
				//#ifdef MP-WEIXIN
				uni.showToast({title: '登录成功!',icon:'none'});
				//跳转首页
				setTimeout(() => {
					outerHref('/pages/index/index',2)
				}, 1000)
				//#endif
			}
		},
		
		//获取用户数据startTrackFun
		async getUserInfoFun(){
			await getUserInfo()
			.then((res)=>{
				this.userInfo=res;
				uni.setStorageSync('userInfo',res);
			})
		},
		
		//获取用户数据 猎鹰
		async startTrackFun(){
			await getUserInfo()
			.then((res)=>{
				console.log(res,'获取用户数据 猎鹰')
				aMapTrack(res.track_info.sid,res.track_info.tid,res.track_info.trid);
			})
		},
		//定位位置
		addressGetLocation(callback) {
			uni.getLocation({
					type: 'gcj02',
					isHighAccuracy:true,
					geocode:true,
					success: function (res) {
						callback(res,true);
					},
					fail: error => {
						callback(error,false);
					}
			});	
		},
		
		//退出登录
		outLogin(){
			uni.removeStorageSync("token");
			uni.removeStorageSync("userInfo");
			this.userInfo=null;
			uni.showToast({
				title: '退出成功！',
				icon: 'none',
				complete: () => {
					uni.reLaunch({
						 url: '/pagesA/pages/login/login'
					})
				}
			});
		},
		
	}
})
 
//暴露用户小仓库
export default useUserStore